{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv \n",
    "\n",
    "with open('tb.csv','a',newline=\"\",encoding='utf-8') as file:\n",
    "    res=csv.writer(file,delimiter=',')\n",
    "    res.writerow([45,28,3,4])\n",
    "    res.writerow([5659,6,5437,8])\n",
    "    \n",
    "    file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['店铺', '信息', '付款人数', '价格']\n",
      "[]\n",
      "['djldjfl', 'dff', '787', '的垃圾分类']\n",
      "[]\n",
      "['1', '2', '3', '4']\n",
      "['5', '6', '7', '8']\n",
      "['45', '28', '3', '4']\n",
      "['5659', '6', '5437', '8']\n"
     ]
    }
   ],
   "source": [
    "import csv \n",
    "\n",
    "with open('tb.csv','r') as file:\n",
    "    res=csv.reader(file)\n",
    "    for i in res:\n",
    "        print(i)\n",
    "    file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始啃骨头了\n",
      "\u001b[31:lm[consumer]fujun\u001b[0m\n",
      "开始啃骨头了\n",
      "\u001b[31:lm[consumer]fuwen\u001b[0m\n",
      "\u001b[31:lm[producer]\u001b[0m正在生产骨头1\n",
      "[fujun]正在啃骨头1\n",
      "\u001b[31:lm[consumer]fujun\u001b[0m\n",
      "[fuwen]正在啃骨头1\n",
      "\u001b[31:lm[consumer]fuwen\u001b[0m\n",
      "\u001b[31:lm[producer]\u001b[0m正在生产骨头2\n",
      "[fujun]正在啃骨头2\n",
      "\u001b[31:lm[consumer]fujun\u001b[0m\n",
      "[fuwen]正在啃骨头2\n",
      "\u001b[31:lm[consumer]fuwen\u001b[0m\n",
      "\u001b[31:lm[producer]\u001b[0m正在生产骨头3\n",
      "[fujun]正在啃骨头3\n",
      "\u001b[31:lm[consumer]fujun\u001b[0m\n",
      "[fuwen]正在啃骨头3\n",
      "\u001b[31:lm[consumer]fuwen\u001b[0m\n",
      "\u001b[31:lm[producer]\u001b[0m正在生产骨头4\n",
      "[fujun]正在啃骨头4\n",
      "\u001b[31:lm[consumer]fujun\u001b[0m\n",
      "[fuwen]正在啃骨头4\n",
      "\u001b[31:lm[consumer]fuwen\u001b[0m\n",
      "\u001b[31:lm[producer]\u001b[0m正在生产骨头5\n",
      "[fujun]正在啃骨头5\n",
      "\u001b[31:lm[consumer]fujun\u001b[0m\n",
      "[fuwen]正在啃骨头5\n",
      "\u001b[31:lm[consumer]fuwen\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "def consumer(name):\n",
    "    print('开始啃骨头了')\n",
    "    while True:\n",
    "        print('\\033[31:lm[consumer]%s\\033[0m'%name)\n",
    "        bone=yield\n",
    "        print('[%s]正在啃骨头%s'%(name,bone))\n",
    "def producer(obj1,obj2):\n",
    "    obj1.send(None)\n",
    "    obj2.send(None)\n",
    "    n=0\n",
    "    while n<5:\n",
    "        n+=1\n",
    "        print('\\033[31:lm[producer]\\033[0m正在生产骨头%s'%n)\n",
    "        obj1.send(n)\n",
    "        obj2.send(n)\n",
    "if __name__=='__main__':\n",
    "    con=consumer('fujun')\n",
    "    conn=consumer('fuwen')\n",
    "    producer(con,conn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "789\n",
      "456\n",
      "666\n"
     ]
    }
   ],
   "source": [
    "from greenlet import greenlet\n",
    "def text():\n",
    "    print('123')\n",
    "    two.switch()\n",
    "    print('456')\n",
    "    two.switch()\n",
    "def test():\n",
    "    print('789')\n",
    "    one.switch()\n",
    "    print('666')\n",
    "one=greenlet(text)\n",
    "two=greenlet(test)\n",
    "one.switch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "synchronous\n",
      "task 1 dons\n",
      "task 2 dons\n",
      "task 3 dons\n",
      "task 4 dons\n",
      "task 5 dons\n",
      "task 6 dons\n",
      "task 7 dons\n",
      "task 8 dons\n",
      "task 9 dons\n",
      "asynchronous\n",
      "task 0 dons\n",
      "task 1 dons\n",
      "task 2 dons\n",
      "task 3 dons\n",
      "task 4 dons\n",
      "task 5 dons\n",
      "task 6 dons\n",
      "task 7 dons\n",
      "task 8 dons\n",
      "task 9 dons\n"
     ]
    }
   ],
   "source": [
    "import gevent\n",
    "def task(pid):\n",
    "    gevent.sleep(0.5)\n",
    "    print('task %s dons'%pid)\n",
    "def synchronous():\n",
    "    for i in range(1,10):\n",
    "        task(i)\n",
    "def asynchronous():\n",
    "    threads=[gevent.spawn(task,i) for i in range(10)]\n",
    "    gevent.joinall(threads)\n",
    "print('synchronous')\n",
    "synchronous()\n",
    "print('asynchronous')\n",
    "asynchronous()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
